/*eslint-disable*/
/*
* 表单打印 私有方法
* */
const pfFunPrivate = {
  //关闭窗口
  close() {
    this.$emit('printFormClose')
  },

  //打印机选择后
  selectChange(name) {
    this.printer = this.printers.find(item => item.name == name)
    this.printerSelPage = null
    this.$refs.printerPage.focus()
    this.utilPrint.updateWorkSet(name)
    this.utils.updateUserState('printerFormName', name, this)
  },
  //打服务器选择后
  printerServerChange(id) {
    this.printerServer = this.printerServers.find(item => item.id == id)
    this.utils.updateUserState('printer_form_serverid', id, this)
  },

  //纸张选择后
  selectPageChange(name) {
    this.utils.updateUserState('printerFormPage', name, this)
    let pageSize = global.utilPrint.getPaperSize(name)
    if(!pageSize) {
      this.utilMessage.warning('您所选择的纸张【'+name+'】未能找到有效的尺寸，请自行设置')
      this.$refs.printerPageWidth.focus()
      return
    }
    this.printerPageWidth = pageSize.width
    this.printerPageHeight = pageSize.height
    this.printerPageHeightChange(this.printerPageHeight)
  },

  //高度选择后
  printerPageHeightChange(name) {
    /**根据纸张高度计算每页数量
     * 基本上 1毫米 约等于 3.78像素
     * 每页数量（向下取整）=（纸张高度-非数据表格数据）/单行高度
     */
    let maxH=this.printerPageHeight*this.mmRatePx
    if($('.title:first').length >0) maxH -= $('.title:first').height()
    if($('.head:first').length >0) maxH -= $('.head:first').height()
    if($('.dataTableHead:first').length >0) maxH -= $('.dataTableHead:first').height()
    if($('.dataTableHead:first').length >0) maxH -= $('.dataTableHead:first').height()
    if($('.sum:first').length >0) maxH -= $('.sum:first').height()
    if($('.foot:first').length >0) maxH -= $('.foot:first').height()
    this.printerPageSize = parseInt(maxH/50)
  },

  //方向选择后
  selectOrientChange(name) {
    if (name == '2') { //横向打印
      //宽度小于高度，则进行逆转
      if (this.printerPageWidth < this.printerPageHeight) {
        let printerPageWidthTmp = this.printerPageWidth
        this.printerPageWidth = this.printerPageHeight
        this.printerPageHeight = printerPageWidthTmp
        this.printerPageHeightChange(this.printerPageHeight)
      }
    } else{//纵向打印
      //宽度大于高度，则进行逆转
      if (this.printerPageWidth > this.printerPageHeight) {
        let printerPageWidthTmp = this.printerPageWidth
        this.printerPageWidth = this.printerPageHeight
        this.printerPageHeight = printerPageWidthTmp
        this.printerPageHeightChange(this.printerPageHeight)
      }
    }
  },

  //打印或者预览
  async print(ispRreview) {
    if (!this.printerSelId) {
      this.utilMessage.warning('请选择打印机')
      this.$refs.printer.focus()
      return
    }
    if (!this.printerSelPage) {
      this.utilMessage.warning('请选择纸张')
      this.$refs.printerPage.focus()
      return
    }
    if (!this.printerSelOrient) {
      this.utilMessage.warning('请选择方向')
      this.$refs.printerOrient.focus()
      return
    }

    this.utilVue.loadShow(true)
    await this.initDataForConfigList()

    console.log('configListaaaa', this.configList)
    this.currentUser = this.utils.getCurrentUser()

    let that = this
    setTimeout( () => {
      if (!that.cpIsStaticHtml) {
        //分页
        $('.pagebreak').remove()
        let dataTableNum = $('.dataTable').length
        if(dataTableNum >1){
          $('.dataTableBox').append('<div class="pagebreak" style="page-break-after:always">&nbsp;&nbsp;&nbsp;</div>')
          $('.pagebreak:last').remove()
        }
      }

      //设置打印机属性
      that.userLodopModel.printerName = that.printerSelId
      that.userLodopModel.printerOrient = that.printerSelOrient
      that.userLodopModel.printerPageWidth = that.printerPageWidth
      that.userLodopModel.printerPage = that.printerSelPage
      that.userLodopModel.printerPageHeight = that.printerPageHeight

      let printBox = $(that.$refs.printBox)

      that.userLodopModel.addPrintHtml(printBox.prop("outerHTML"))
      let retMsg = null

      //设置打印机
      var ret3=that.userLodopModel.lodop.SET_PRINTER_INDEXA(that.printerSelId);
      if(!ret3) {
        global.utilMessage.alertFail(`目标打印机不可使用`, '系统提示', {
          confirmButtonText: '确定',
          type: 'warning',
          callback: action => {
          }
        })
        return
      }

      //虚拟打印机，指定纸张宽度
      if (that.printerSelId.toLowerCase().indexOf('pdf') != -1 || that.printerSelId.toLowerCase().indexOf('onenote') != -1
          || that.printerSelId.toLowerCase().indexOf('xps') != -1 || that.printerSelId.toLowerCase().indexOf('fax') != -1) {
        that.userLodopModel.lodop.SET_PRINT_PAGESIZE(that.printerSelOrient, that.printerPageWidth, 0,that.printerPageHeight);
      } else { //真实打印机，指定纸张名称
        that.userLodopModel.lodop.SET_PRINT_PAGESIZE(that.printerSelOrient,0,that.printerPageHeight,that.printerSelPage);
      }

      if (ispRreview) {
        that.utilVue.loadHide()
        //预览窗口已打开
        if(that.userLodopModel.lodop.OBO_Mode && that.userLodopModel.lodop.blOneByone) {
          global.utilMessage.alertFail(`预览窗口已打开，请到任务栏找到打印预览图标进行关闭，然后重新尝试预览操作`, '系统提示', {
            confirmButtonText: '确定',
            type: 'warning',
            callback: action => {
            }
          })
          return
        }

        that.userLodopModel.lodop.SET_PREVIEW_WINDOW(1,2,1,1000,800,"打印预览");

        //预览窗口禁止最小化，并始终最前
        that.userLodopModel.lodop.SET_SHOW_MODE("PREVIEW_NO_MINIMIZE",true)
        that.userLodopModel.lodop.PREVIEW();
      } else {
        // retMsg = this.userLodopModel.print()

        that.userLodopModel.lodop.SET_PRINT_MODE("CATCH_PRINT_STATUS",true)

        let printValue = null

        that.userLodopModel.lodop.On_Return=function(TaskID,Value){
          printValue = Value

          var strResult
          try{
            strResult=that.userLodopModel.lodop.GET_VALUE('PRINT_STATUS_ID',printValue)
          }catch (e) {
            console.error('GET_VALUE',e)
          }
          setTimeout(() => {
            that.utilMessage.success('打印任务已提交')
            that.utilVue.loadHide()
            if ( that.printAuto) that.close()
          }, 1000)

        };
        let ret = that.userLodopModel.lodop.PRINT();

        if (this.param.tableName && this.param.pkName && this.param.pkValue){
          global.utilPrint.updateFormPrintTime(this.param.tableName,this.param.pkName,this.param.pkValue)
        }
      }
    },1000)
  },

  //查询打印服务器
  async getPrinterServers(){
    //printType
    let wrapper = this.$newWrapper()

    if (this.printType == 'form') {
      wrapper.eq('typeKey', 'form')
    } else {
      wrapper.eq('typeKey', 'rfid').or('typeKey', 'label')
    }

    const params = {
      queryString: this.utils.json2str(wrapper.queryConditions)
    }
    await this.$ajax.get('/sys/printer/list',{params}).then(resp => {
      if (resp.code == 0) {
        this.printerServers = resp.data
      }
    })
  }
}

export default pfFunPrivate
